/**
 */
;
(function(){
'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('WarningAddScreenCtrl', ['$scope','$modalInstance','Modal','WarningAddService',
	   function($scope,$modalInstance,Modal,WarningAddService){
		  //封装数据
	   	  $scope.warnscreen = {};
//	   	  $scope.warnMonitorList=[];
//	   	  $scope.warnRuleList=[];
//	   	  $scope.storeWarnRuleList=[];
	   	  function init(){
	    	  WarningAddService.getWarnMonitorList().success(function(response){
	    		  $scope.warnMonitorList =response;
	    	  });
	    	  WarningAddService.getWarnRuleList().success(function(response){
	    		  //$scope.warnRuleList=response;
	    		  $scope.storeWarnRuleList=response;
	    	  });
	       }
	   	  init();
	   	  $scope.changeMonitor = function(){
	   		  if($scope.warnscreen.monitor){
	   			  $scope.warnscreen.monitorNo=$scope.warnscreen.monitor.split('|')[0];
	   			changeRuleOnChangeMonitor();
	   		  }else{
	   			  $scope.warnscreen.monitorNo=null;
	   		  }
	   	  }
	   	  $scope.changeRule = function(){	   		  
	   			if($scope.warnscreen.rule){
		   			$scope.warnscreen.ruleNo=$scope.warnscreen.rule.split('|')[0];
		   		}else{
		   			$scope.warnscreen.ruleNo=null;
		   		}
	   		    		
	   	  }
	   	  //改变监控类型改变规则名称
	   	  function changeRuleOnChangeMonitor(){
	   		var newWarnRuleList=[];
	   		angular.forEach($scope.storeWarnRuleList,function(data,index){
				   if(data.monitorNo==$scope.warnscreen.monitorNo){ 
					   newWarnRuleList.push(data);					  
				   }
			   });	
	   	 $scope.warnRuleList=newWarnRuleList;
	   	  }
    	  //保存
	   	  $scope.confirm = function (){
	   		  var monitorNo=null;
	   		  var monitorType=null;
    		 if(!$scope.warnscreen.monitor){
    			 Modal.alert("监控类型不能为空!");
    			 return;
    		 } else{
    			 monitorNo=$scope.warnscreen.monitor.split('|')[0];
    			 monitorType=$scope.warnscreen.monitor.split('|')[1];
    		 }
    		 var ruleNo=null;
    		 var ruleName=null
    		 if(!$scope.warnscreen.rule){
    			 Modal.alert("规则名称不能为空!");
    			 return;
    		 }else{
    			 ruleNo=$scope.warnscreen.rule.split('|')[0];
    			 ruleName=$scope.warnscreen.rule.split('|')[1];
    		 }
//    		 if(!$scope.warnscreen.warnValue){
//    			 Modal.alert("阀值不能为空!");
//    			 return;
//    		 }
    		/* if(!$scope.warnscreen.remark){
    			 Modal.alert("备注不能为空!");
    			 return;
    		 }*/
    		 var param = {
    			 monitorType:monitorType,
    			 monitorNo:monitorNo,
    			 ruleName:ruleName,
    			 ruleNo:ruleNo,
    			 warnValue:$scope.warnscreen.warnValue,
    			 remark:$scope.warnscreen.remark
    		 };
		     $modalInstance.close(param);
    	  }  
    	  //取消或或者直接关闭
    	  $scope.onOk = function(){
  		   $modalInstance.dismiss();
  	    }	
	}]).
	controller('WarningAddMedicalCtrl',
			['$scope','$modalInstance', 'Modal','WarningAddService','CommunityService', 
			 function($scope,$modalInstance, Modal,WarningAddService,CommunityService){
				$scope.serv = WarningAddService;
				$scope.communityService = CommunityService;
				CommunityService.initCodeListDatas()
				WarningAddService.initMedicalOrgInfoList();
				$scope.type = true;
				
				$scope.searchDatas = ['medicalOrgName','namePysx','nameWubi'];
				$scope.medicalData = 'medicalOrgName';
				$scope.medical = {};
				$scope.tableData=[];

				$scope.columns = [{label:'机构编号', map:'code', width:'25%'},
				                  {label:'机构名称',map:'name',width:'25%'},
				                  {label:'机构类型',map:'type',width:'25%'},
				                  {label:'机构等级',map:'level',width:'25%'}];

				 var page = 1,
		             limit = 10;
				 //页面查询
				 $scope.queryMedical = function (){
					 page = 1;
					 $scope.currentPage=1;
					 query();					
				  }
				 //查询
				 function query(){
					 var params = {
								orgCode:$scope.medical.orgCode,
								orgName:$scope.medical.orgName,
								orgType:$scope.medical.orgType,
								orgLevel:$scope.medical.orgLevel,
								page:page,
								limit:limit};
					WarningAddService.getWarningMedicalList(params).
						success(function(res){
							 $scope.tableData = res.result;
				             $scope.totalCount = res.total;
						});
				 }
				//分页查询
		        $scope.$on('pageTool.skipPage',function(e,v){
		            page = v;
		            $scope.currentPage = v;
		            query();
		         });
			        //加载数据
			        //$scope.queryMedical();

		    	  //保存
		    	  $scope.confirm = function (){
		    		  var num = 0;
		    		  var isFlat = true;
		    		  var resultArray = {};
		    		  angular.forEach($scope.tableData,function(data,index){
						   if(data.checked){ 
							   resultArray=data; 
							   num++;
						   }
					   });
		    		  if(num == 0){
		    			  Modal.alert("尚未勾选信息！");
							return null;
		    		  }else if(num > 1){
		    			  Modal.alert("只能选择一条医疗机构！");
							return;
		    		  }
	    			  $modalInstance.close(resultArray);
		    	  }  
				$scope.onClose = function(){
    	    		$modalInstance.dismiss();
    	    	}
				//重置
				$scope.onReset = function (){
					$scope.medical={};
				}
				
	}]).controller('WarningAddStaffCtrl',
			['$scope','$modalInstance', 'Modal','CommunityService','WarningAddService', 
			 function($scope,$modalInstance, Modal,CommunityService,WarningAddService){
				$scope.serv = WarningAddService;
				$scope.communityService = CommunityService;
				WarningAddService.initMedicalOrgInfoList();
				$scope.staff = {};
				$scope.tableData = [];
				$scope.checkedData=[];
				$scope.queryStaff = function (){
					var isNameEmpty=!$scope.staff.name;
					var isOrgCodeEmpty=!$scope.staff.orgCode;
					var isCardNoEmpty=!$scope.staff.cardNo;
					var isPersonNoEmpty=!$scope.staff.personNo;
				
					if(isNameEmpty&&isOrgCodeEmpty&&isPersonNoEmpty&&isCardNoEmpty){
						Modal.alert("必须输入一个查询条件。");
						return null;
					}
					var param={name:$scope.staff.name,
							   orgCode:$scope.staff.orgCode,
							   personNo:$scope.staff.personNo,
							   cardNo:$scope.staff.cardNo};
					WarningAddService.getWarningStaffList(param).
					success(function(res){
						$scope.tableData = res.result;
					});
				}
				
				$scope.onItemClick = function(data) {
					data.selected = data.selected == true?false:true;
				}
				
				function add(src, tar) {
					var selected = [], unselected = [];
					$.each(src, function(){
						if(this.selected) {
							selected.push(this);
							this.selected = false;
						} else {
							unselected.push(this);
						}
					})
					return [unselected, tar.concat(selected)];
				}
				function addAll(src, tar) {
					return $.grep(src, function(entry) {
						if ($.inArray(entry, tar) != -1) {
							return true;
						}
						entry.selected = false;
						tar.push(entry);
						return false;
					});
				}
				
				function delAll(src, tar) {
					return $.grep(src, function(entry) {
						if ($.inArray(entry, tar) != -1) {
							return true;
						}
						entry.selected = false;
						tar.push(entry);
						return false;
					});
				}
				$scope.selectUserInfoList= [];
				//关闭model
				$scope.onClose = function() {
					$modalInstance.close();
				}
				//添加所有
				$scope.addAll=function(){
					$scope.tableData =addAll($scope.tableData, $scope.checkedData);
				} 
		        $scope.add=function(){
		        	var arrs = add($scope.tableData, $scope.checkedData);
		        	$scope.tableData = arrs[0];
		        	$scope.checkedData = arrs[1];
				}
		        $scope.del=function(){
		        	var arrs = add($scope.checkedData, $scope.tableData);
		        	$scope.checkedData = arrs[0];
		        	$scope.tableData = arrs[1];
				}
		        $scope.delAll=function(){
		        	$scope.checkedData=delAll($scope.checkedData, $scope.tableData);
				}
				 //保存
		    	  $scope.confirm = function (){
		    		  var num = 0;
		    		  var isFlat = true;
		    		  var resultArray = [];
		    		  angular.forEach($scope.checkedData,function(data,index){
							   resultArray.push(data); 
							   num++;
					   });
		    		  if(num == 0){
		    			  Modal.alert("尚未勾选信息！");
							return null;
		    		  }
	    			  $modalInstance.close(resultArray);
		    	  }  
				$scope.onClose = function(){
    	    		$modalInstance.dismiss();
    	    	}
				//重置
				$scope.onReset = function (){
					//$scope.form={};
					$scope.staff = {};
				}
	}]).controller('WarningAddInsurCtrl',
			['$scope','$modalInstance', 'Modal','WarningAddService','CommunityService', 
			 function($scope,$modalInstance, Modal,WarningAddService,CommunityService){
				$scope.serv = WarningAddService;
				$scope.communityService = CommunityService;
				CommunityService.initCodeListDatas();
				$scope.insurance = {};
				$scope.tableData=[];
				$scope.columns = [{label:'社保号',map:'code',width:'10%'},
				                  {label:'姓名',map:'name',width:'10%'},
				                  {label:'性别',map:'sex',width:'10%'},
				                  {label:'年龄',map:'age',width:'10%'},
				                  {label:'出生日期',map:'birth',width:'15%'},
				                  {label:'医保卡号',map:'otherCardNo',width:'15%'},
				                  {label:'身份证',map:'personNo',width:'15%'},
				                  {label:'人员类型',map:'typeName',width:'15%'}];

				 var page = 1,
		             limit = 10;
				 //页面查询
				$scope.queryInsurance = function (){
					page = 1;
		            $scope.currentPage = 1;
		            query();
				}
				//查询
				function query(){
					var params = {
							name:$scope.insurance.name,
							pcNo:$scope.insurance.pcNo,
							personNo:$scope.insurance.personNo,
							personType:$scope.insurance.personType,
							page:page,
							limit:limit};
					WarningAddService.getWarningInsurList(params).
					success(function(res){
						$scope.tableData = res.result;
						$scope.totalCount = res.total;
					});
				}
				//分页查询
		        $scope.$on('pageTool.skipPage',function(e,v){
		            page = v;
		            $scope.currentPage = v;
		            query();
		         });
				//保存
		    	  $scope.confirm = function (){
		    		  var num = 0;
		    		  var resultArray = [];
		    		  angular.forEach($scope.tableData,function(data,index){
						   if(data.checked){
							   resultArray.push(data);
							   num++;
						   }
					   });
		    		  if(num == 0){
		    			  Modal.alert("尚未勾选信息！");
							return null;
		    		  }
				     $modalInstance.close(resultArray);
		    	  }  
				$scope.onClose = function(){
    	    		$modalInstance.dismiss();
    	    	}
				//重置
				$scope.onReset = function (){
					$scope.insurance={};
				}
	}]).controller('WarningAddNoticeCtrl', ['$scope','$modalInstance','WarningAddService','Modal',
	    function ($scope,$modalInstance,WarningAddService,Modal) {
		$scope.warningAddService=WarningAddService;
		$scope.warningNotice = {};
		$scope.warningNotice.date={};
		if($scope.warnNotice){
			if($scope.warnNotice.datestart){
				$scope.warningNotice.date.start=$scope.warnNotice.datestart;
			}
			if($scope.warnNotice.dateend){
				$scope.warningNotice.date.end=$scope.warnNotice.dateend;
			}
			if($scope.warnNotice.noticeNo){
				$scope.warningNotice.noticeNo=$scope.warnNotice.noticeNo;
			}
			if($scope.warnNotice.name){
				$scope.warningNotice.name=$scope.warnNotice.name;
			}
			if($scope.warnNotice.content){
				$scope.warningNotice.content=$scope.warnNotice.content;
			}
			if($scope.warnNotice.requirement){
				$scope.warningNotice.requirement=$scope.warnNotice.requirement;
			}
			if($scope.warnNotice.noticeDate){
				$scope.warningNotice.noticeDate=$scope.warnNotice.noticeDate;
			}
			if($scope.warnNotice.regionName){
				$scope.warningNotice.regionName=$scope.warnNotice.regionName;
			}
		}		  	
		//关闭事件
		$scope.onClose = function() {					
			$modalInstance.dismiss();
		}	
		//保存事件
		$scope.warningNoticeDialog = function() {
			var dateReg =/^\d{4}-\d{2}-\d{2}$/; 
			if(!$scope.warningNotice.content)
			{
				Modal.alert("预警内容不能为空!");
				return;
			}
			
			if(!dateReg.test($scope.warningNotice.date.start))
			{
				Modal.alert("预警开始时段不能为空,并且必须输入正确的日期格式(yyyy-MM-dd)!");
				return;
			}
			if(!dateReg.test($scope.warningNotice.date.end))
			{
				Modal.alert("预警结束时段不能为空,并且必须输入正确的日期格式(yyyy-MM-dd)!");
				return;
			}
			if(!$scope.warningNotice.requirement)
			{
				Modal.alert("预警要求不能为空!");
				return;
			}
			if(!dateReg.test($scope.warningNotice.noticeDate))
			{
				Modal.alert("日期不能为空,并且必须输入正确的日期格式(yyyy-MM-dd)!");
				return;
			}
			var date=$scope.warningNotice.date;
			var param={
					name:$scope.warningNotice.name,
					content:$scope.warningNotice.content,
					datestart:date.start,
					dateend:date.end,
					noticeDate:$scope.warningNotice.noticeDate,
					requirement:$scope.warningNotice.requirement,
					warnInscribe:$scope.warningNotice.warnInscribe,
					regionName:$scope.warningNotice.regionName,
					noticeNo:$scope.warningNotice.noticeNo
			 }
			$modalInstance.close(param);
		}
	}]).controller('WarningAddResultNoticeCtrl', ['$scope','$modalInstance','WarningAddService','Modal',
	    function ($scope,$modalInstance,WarningAddService,Modal) {
		$scope.warningAddService=WarningAddService;
		$scope.resultNotice ={};
		$scope.resultNotice.date={};
		if($scope.warnNotice){
			if($scope.warnNotice.startDate){
				$scope.resultNotice.date.start=$scope.warnNotice.startDate;
			}
			if($scope.warnNotice.endDate){
				$scope.resultNotice.date.end=$scope.warnNotice.endDate;
			}
			if($scope.warnNotice.name){
				$scope.resultNotice.name=$scope.warnNotice.name;
			}
			if($scope.warnNotice.phone){
				$scope.resultNotice.phone=$scope.warnNotice.phone;
			}
			if($scope.warnNotice.address){
				$scope.resultNotice.address=$scope.warnNotice.address;
			}
			if($scope.warnNotice.problem){
				$scope.resultNotice.problem=$scope.warnNotice.problem;
			}
			if($scope.warnNotice.decide){
				$scope.resultNotice.decide=$scope.warnNotice.decide;
			}
			if($scope.warnNotice.amount){
				$scope.resultNotice.amount=$scope.warnNotice.amount;
			}
			if($scope.warnNotice.noticeDate){
				$scope.resultNotice.noticeDate=$scope.warnNotice.noticeDate;
			}
		}	
	    //关闭事件
	    $scope.onClose = function() {					
		   $modalInstance.dismiss();
	    }					  
	     //保存事件
	    $scope.saveResultNotice = function() {
	    	var dateReg =/^\d{4}-\d{2}-\d{2}$/; 
		   if(!$scope.resultNotice.phone)
			{
				Modal.alert("联系电话不能为空!");
				return;
			}
			if(!$scope.resultNotice.address)
			{
				Modal.alert("联系地址不能为空!");
				return;
			}
			
			if(!dateReg.test($scope.resultNotice.date.start))
			{
				Modal.alert("预警开始时段不能为空,并且必须输入正确的日期格式(yyyy-MM-dd)!");
				return;
			}
			if(!dateReg.test($scope.resultNotice.date.end))
			{
				Modal.alert("预警结束时段不能为空,并且必须输入正确的日期格式(yyyy-MM-dd)!");
				return;
			}
			if(!$scope.resultNotice.problem)
			{
				Modal.alert("存在问题不能为空!");
				return;
			}
			if(!$scope.resultNotice.decide)
			{
				Modal.alert("处理决定不能为空!");
				return;
			}
			if(!$scope.resultNotice.amount)
			{
				Modal.alert("审核不合理费用不能为空!");
				return;
			}
			var canonical=/^\d+(\.\d{1,2})?$/;					
			if(!canonical.test($scope.resultNotice.amount)){
				Modal.alert("审核不合理费用必须是数字(最多两位小数)!");
				return;
			}
			if(!dateReg.test($scope.resultNotice.noticeDate))
			{
				Modal.alert("日期不能为空,并且必须输入正确的日期格式(yyyy-MM-dd)!");
				return;
			}
			var date=$scope.resultNotice.date;
			var param={
					name:$scope.resultNotice.name,
					phone:$scope.resultNotice.phone,
					address:$scope.resultNotice.address,
					startDate:date.start,
					endDate:date.end,
					problem:$scope.resultNotice.problem,
					decide:$scope.resultNotice.decide,
					amount:$scope.resultNotice.amount,
					noticeDate:$scope.resultNotice.noticeDate,
					warnInscribe:$scope.resultNotice.warnInscribe
			}
			$modalInstance.close(param);
	  }
	}]);
})();